﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace BSC_OTC_Management.Forms.SoTraiChu
{
    public partial class Frm_TraSo : Form
    {

        #region Khai bao bien
        Code.CL_Ultility _cl = new Code.CL_Ultility();
        string idso = "", maso = "";
        string matp = "";
        string filename = "";
        #endregion

        public Frm_TraSo()
        {
            InitializeComponent();
        }

        private void btt_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btt_Confr_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_maso.Text.Length != 0)
                {
                    //tạo input data table cho package
                    DataTable table = new DataTable("noname");
                    table.Columns.Add("account", typeof(String));
                    table.Columns.Add("idso", typeof(String));
                    table.Columns.Add("notes", typeof(String));
                    table.Columns.Add("ngaychungtu", typeof(String));
                    table.Rows.Add(Code.StaticVariable.username, idso, txt_diengiai.Text,
                                    DateTime.ParseExact(dtp_ngaychungtu.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy"));//rowData
                    table.Rows.Add("T","T","T","D");//rowType
                    //ten store
                    string store = "bsc_205.Tra_so_trai_chu";
                    string _result = _cl._services.Excute_Store_Transaction(store, table);
                    if (_result == "0")
                    {
                        MessageBox.Show(Code.StaticVariable.messeseboxThanhCong, Code.StaticVariable.messeseboxLuuY);
                        _cl.ClearAllText(this);
                    }
                    else
                    {
                        MessageBox.Show(_result, Code.StaticVariable.messeseboxLuuY);
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Code.StaticVariable.messeseboxLuuY);
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                try
                {
                    //chuyển sang Form View để tìm kiếm Sổ trái chủ
                    Forms.FrmView _FrmView = new Forms.FrmView("BSC_TBL_GIAONHANSOTRAICHU", "Thông tin Giao nhận của sổ trái chủ", true);
                    _FrmView.ShowDialog();
                    //lấy về dữ liệu DataTable formView Sotraichu
                    DataTable dt = Code.StaticVariable._tbl;

                    if (dt.Columns.Count >= 5 && dt.Rows.Count >= 1)
                    {
                        idso = dt.Rows[0]["autoid"].ToString();
                        maso = dt.Rows[0]["maso"].ToString();
                    }
                    Code.StaticVariable._tbl.Clear();
                    string sql = "Select st.maso, st.makh, kh.hoten,kh.cmt, kh.gpkd, kh.dienthoai, ac.cdcontent loaikh, st.matp, st.slphothong "
                               + "from bsc_tbl_khachhang kh, bsc_tbl_sotraichu st "
                               + "inner join allcode ac on ac.cdname='LOAIKH' and ac.cdtype= 'BSC_TBL_KHACHHANG' "
                               + "where st.makh = kh.makh and st.maso = '" + maso + "' "
                               + "and kh.loaikh = ac.cdval";

                    DataTable result = _cl._services.GetDatatable_BySQL(sql);
                    if (result.Columns.Count >= 7 && result.Rows.Count >= 1)
                    {
                        txt_maso.Text = result.Rows[0]["maso"].ToString();
                        txt_makh.Text = result.Rows[0]["makh"].ToString();
                        txt_hoten.Text = result.Rows[0]["hoten"].ToString();
                        txt_cmt.Text = result.Rows[0]["cmt"].ToString() + result.Rows[0]["gpkd"].ToString();
                        txt_dienthoai.Text = result.Rows[0]["dienthoai"].ToString();
                        txt_loaikh.Text = result.Rows[0]["loaikh"].ToString();
                        matp = result.Rows[0]["matp"].ToString();
                        txt_matp.Text = matp;
                        txt_sodu.Text = result.Rows[0]["slphothong"].ToString();
                    }

                    string sql1 = "Select tc.tcphid, tc.ten, tp.matp, tp.tentp, tp.maugiaycn from bsc_tbl_tpph tp, bsc_tbl_hopdongdaily hd, bsc_tbl_tcph tc "
                        + "where tp.sohd = hd.sohd and hd.tcphid = tc.tcphid and tp.matp = '" + matp + "'";
                    DataTable result1 = _cl._services.GetDatatable_BySQL(sql1);
                    if (result1.Rows.Count >= 1)
                    {
                        txt_tcph.Text = result1.Rows[0]["ten"].ToString();
                        txt_tentp.Text = result1.Rows[0]["tentp"].ToString();
                        filename = result1.Rows[0]["maugiaycn"].ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }
}
